package cfca.seal.front.service.servlet;

import cfca.sadk.util.Base64;
import cfca.seal.front.service.FunctionType;
import cfca.seal.front.service.ws.IAssistSealService;
import cfca.seal.util.StringUtil;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class AssistSealServlet extends HttpServlet
{
  private static final long serialVersionUID = -2802334706155081655L;
  private static final Logger logger = LoggerFactory.getLogger("service_sys");

  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    request.setCharacterEncoding("UTF-8");
    String type = request.getParameter("type");

    OutputStream os = response.getOutputStream();
    try {
      ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
      IAssistSealService assistSealService = (IAssistSealService)applicationContext.getBean("assistSealServiceImpl");

      if (type.equals(FunctionType.p1Sign.toString()))
      {
        String sourceString = request.getParameter("source");
        byte[] source = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(sourceString)) {
          source = Base64.decode(sourceString.getBytes("UTF-8"));
        }
        String signStrategyXML = request.getParameter("signStrategyXML");
        if (StringUtil.isNotEmpty(signStrategyXML)) {
          signStrategyXML = new String(Base64.decode(signStrategyXML.getBytes("UTF-8")), "UTF-8");
        }
        String p1Sign = assistSealService.p1Sign(source, signStrategyXML);
        byte[] result = Base64.encode(p1Sign.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.p7SignDetached.toString()))
      {
        String sourceString = request.getParameter("source");
        byte[] source = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(sourceString)) {
          source = Base64.decode(sourceString.getBytes("UTF-8"));
        }
        String signStrategyXML = request.getParameter("signStrategyXML");
        if (StringUtil.isNotEmpty(signStrategyXML)) {
          signStrategyXML = new String(Base64.decode(signStrategyXML.getBytes("UTF-8")), "UTF-8");
        }
        String p7SignDetached = assistSealService.p7SignDetached(source, signStrategyXML);
        byte[] result = Base64.encode(p7SignDetached.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.p7VerifyDetached.toString()))
      {
        String signatureBase64 = request.getParameter("signatureBase64");
        if (StringUtil.isNotEmpty(signatureBase64)) {
          signatureBase64 = new String(Base64.decode(signatureBase64.getBytes("UTF-8")), "UTF-8");
        }
        String sourceString = request.getParameter("source");
        byte[] source = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(sourceString)) {
          source = Base64.decode(sourceString.getBytes("UTF-8"));
        }
        String verifyStrategyXML = request.getParameter("verifyStrategyXML");
        if (StringUtil.isNotEmpty(verifyStrategyXML)) {
          verifyStrategyXML = new String(Base64.decode(verifyStrategyXML.getBytes("UTF-8")), "UTF-8");
        }
        String p7VerifyDetached = assistSealService.p7VerifyDetached(signatureBase64, source, verifyStrategyXML);
        byte[] result = Base64.encode(p7VerifyDetached.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.getSealInfo.toString()))
      {
        String sealCode = request.getParameter("sealCode");
        if (StringUtil.isNotEmpty(sealCode)) {
          sealCode = new String(Base64.decode(sealCode.getBytes("UTF-8")), "UTF-8");
        }
        String sealInfo = assistSealService.getSealInfo(sealCode);
        byte[] result = Base64.encode(sealInfo.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.autoGenerateImage.toString()))
      {
        String imageStrategyXML = request.getParameter("imageStrategyXML");
        if (StringUtil.isNotEmpty(imageStrategyXML)) {
          imageStrategyXML = new String(Base64.decode(imageStrategyXML.getBytes("UTF-8")), "UTF-8");
        }
        String autoGenerateImage = assistSealService.autoGenerateImage(imageStrategyXML);
        byte[] result = Base64.encode(autoGenerateImage.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.getBindedSealInfo.toString()))
      {
        String certDN = request.getParameter("certDN");
        if (StringUtil.isNotEmpty(certDN)) {
          certDN = new String(Base64.decode(certDN.getBytes("UTF-8")), "UTF-8");
        }
        String certSN = request.getParameter("certSN");
        if (StringUtil.isNotEmpty(certSN)) {
          certSN = new String(Base64.decode(certSN.getBytes("UTF-8")), "UTF-8");
        }
        String bindedSealInfo = assistSealService.getBindedSealInfo(certDN, certSN);
        byte[] result = Base64.encode(bindedSealInfo.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.makeAndBindClientSeal.toString()))
      {
        String imageDataString = request.getParameter("imageData");
        byte[] imageData = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(imageDataString)) {
          imageData = Base64.decode(imageDataString.getBytes("UTF-8"));
        }
        String clientSealXML = request.getParameter("clientSealXML");
        if (StringUtil.isNotEmpty(clientSealXML)) {
          clientSealXML = new String(Base64.decode(clientSealXML.getBytes("UTF-8")), "UTF-8");
        }
        String userXML = request.getParameter("userXML");
        if (StringUtil.isNotEmpty(userXML)) {
          userXML = new String(Base64.decode(userXML.getBytes("UTF-8")), "UTF-8");
        }
        String orgCode = request.getParameter("orgCode");
        if (StringUtil.isNotEmpty(orgCode)) {
          orgCode = new String(Base64.decode(orgCode.getBytes("UTF-8")), "UTF-8");
        }
        String makeAndBindClientSeal = assistSealService.makeAndBindClientSeal(imageData, clientSealXML, userXML, orgCode);
        byte[] result = Base64.encode(makeAndBindClientSeal.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.getKeywordLocationsInPdf.toString()))
      {
        String pdfString = request.getParameter("pdf");
        byte[] pdf = null;
        if (StringUtil.isNotEmpty(pdfString)) {
          pdf = Base64.decode(pdfString.getBytes("UTF-8"));
        }

        String keyword = request.getParameter("keyword");

        String getKeywordLocationsInPdf = assistSealService.getKeywordLocationsInPdf(pdf, keyword);
        byte[] result = Base64.encode(getKeywordLocationsInPdf.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.bindClientSeal.toString()))
      {
        String clientSealCode = request.getParameter("clientSealCode");
        if (StringUtil.isNotEmpty(clientSealCode)) {
          clientSealCode = new String(Base64.decode(clientSealCode.getBytes("UTF-8")), "UTF-8");
        }
        String userXML = request.getParameter("userXML");
        if (StringUtil.isNotEmpty(userXML)) {
          userXML = new String(Base64.decode(userXML.getBytes("UTF-8")), "UTF-8");
        }
        String orgCode = request.getParameter("orgCode");
        if (StringUtil.isNotEmpty(orgCode)) {
          orgCode = new String(Base64.decode(orgCode.getBytes("UTF-8")), "UTF-8");
        }
        String bindClientSeal = assistSealService.bindClientSeal(clientSealCode, userXML, orgCode);
        byte[] result = Base64.encode(bindClientSeal.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.getPdfPageCount.toString()))
      {
        String pdfString = request.getParameter("pdf");
        byte[] pdf = null;
        if (StringUtil.isNotEmpty(pdfString)) {
          pdf = Base64.decode(pdfString.getBytes("UTF-8"));
        }

        String getPdfPageCount = assistSealService.getPdfPageCount(pdf);
        byte[] result = Base64.encode(getPdfPageCount.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.revokePdfSeal.toString()))
      {
        String pdfString = request.getParameter("pdf");
        byte[] pdf = null;
        if (StringUtil.isNotEmpty(pdfString)) {
          pdf = Base64.decode(pdfString.getBytes("UTF-8"));
        }

        int count = Integer.valueOf(request.getParameter("count")).intValue();

        pdf = assistSealService.revokePdfSeal(pdf, count);
        byte[] result = Base64.encode(pdf);
        os.write(result, 0, result.length);
      }

      if (null != os) {
        os.flush();
        os.close();
      }
    }
    catch (Exception e)
    {
      logger.error("C0001", e);
      StringBuilder result = new StringBuilder();
      result.append("<Error><errorCode>");
      result.append("C0001");
      result.append("</errorCode><ErrorMessage>");
      result.append("错误");
      result.append("</ErrorMessage>");
      response.getOutputStream().write(Base64.encode(result.toString().getBytes("UTF-8")));

      if (null != os) {
        os.flush();
        os.close();
      }
    }
    finally
    {
      if (null != os) {
        os.flush();
        os.close();
      }
    }
  }
}